package com.tisco;


import java.io.*;
import java.net.Socket;

public class TransferClient {
    private String host ;
    private int port ;
    private FileInputStream fis;
    private DataOutputStream dos;

    protected Socket socket;

    // 连接到指定的主机和端口
    public TransferClient(String host, int port)  {
        this.host = host;
        this.port = port;
        try {
            this.socket = new Socket(host, port);
        } catch (IOException e) {
            System.out.println("Server:" + host + ",Port:" + port + " 未启动");
            //System.out.println(e+"ee");
        }
    }

    public void send(String location)  {
        boolean delete = true;
        boolean circleFlag = true;
        while (circleFlag){
            File file = new File(location);
            if(!file.exists()){
                file.mkdir();
            }
            if(file.isDirectory()){
                File[] files = file.listFiles();
                for(int i=0;i<files.length&&delete;i++){
                    File tempFile = files[i];
                    try{
                        fis = new FileInputStream(tempFile);
                        dos = new DataOutputStream(socket.getOutputStream());
                        // 文件名和长度
                        dos.writeUTF(tempFile.getName());
                        dos.flush();
                        dos.writeLong(tempFile.length());
                        dos.flush();
                        // 开始传输文件
                        System.out.println("======== 文件开始上传 ========");
                        int fisAvailable = fis.available();
                        //转成KB
                        if(fisAvailable>1000&&fisAvailable<1000000){
                            System.out.println("文件大小为：\n"+ fisAvailable/1024+"KB");
                        }else if(fisAvailable>=1000000){
                            System.out.println("文件大小为：\n"+ fisAvailable/1048576+"MB");
                        }
                        //IO流缓存，缓存大，读写较快
                        byte[] bytes = new byte[1024];
                        int length = 0;
                        long send = 0;
                        System.out.println("文件已传输：");
                        while((length = fis.read(bytes, 0, bytes.length)) != -1) {
                            dos.write(bytes, 0, length);
                            dos.flush();
                            send += length;
                            System.out.print((send*100)/fisAvailable + "%");
                            for (int j = 0; j <= String.valueOf(send*100/fisAvailable+"%").length(); j++) {
                                System.out.print("\b");
                            }
                        }
                        System.out.println();
                        System.out.println("======== 文件传输成功 ========");
                        if(dos!=null){
                            dos.close();
                        }
                        if(fis!=null){
                            fis.close();
                        }
                        delete = tempFile.delete();
                    }catch (IOException e){
                        System.out.println(e.getMessage());
                    } finally {
                        //文件被锁，强制退出服务
                        if(!delete){
                            System.out.println("此文件被其他进程锁定无法删除或者Server已经关闭，Client已经关闭，重新启动");
                            circleFlag = false;
                         //文件正常退出，重新打开Socket
                        }else {
                            try {
                                if(socket.isClosed()){
                                    socket = new Socket(host, port);
                                }
                            } catch (Exception e) {
                                System.out.println("Server未启动");
                            }
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
//        TransferClient transferClient = new TransferClient("localhost", 8087);
//        boolean startFlag = (transferClient.socket==null);
//        for(int i=0;i<6&&startFlag;i++){
//            System.out.println("10秒之后，重新连接");
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            transferClient = new TransferClient("localhost", 8087);
//            startFlag = (transferClient.socket==null);
//        }
//        if(!startFlag){
//            System.out.println("连接成功");
//            transferClient.send("F:/testScanner");
//        }
    }
}
